﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TuKu
{
    public partial class CategoryMain : Form
    {
        public CategoryMain()
        {
            InitializeComponent();
        }

        private void CategoryMain_Load(object sender, EventArgs e)
        {
            DataBindClass.GroupBind(listGroup);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
           
                if (!String.IsNullOrEmpty(txtGroupTitle1.Text.Trim()))
                {
                    string title = txtGroupTitle1.Text.Trim();
                    BLL.Group bll = new BLL.Group();
                    Model.Group model = new Model.Group();
                    model.AddTime = DateTime.Now;
                    model.OrderID = 0;
                    model.Title = title;
                    model.Pin = Common.Hz2Py.Convert(title);
                    if (bll.GetModel(title) == null)
                    {
                        bll.Add(model);
                        DataBindClass.GroupBind(listGroup);
                        listGroup.SelectedItem = title;
                    }
                   
                }
            

        }

        private void listGroup_DoubleClick(object sender, EventArgs e)
        {
            txtGroupTitle2.Text = listGroup.Text;
            DateTime date = new BLL.Group().GetModel(listGroup.Text).AddTime;
            chkdisplay.Checked = false;
            if(date>Convert.ToDateTime("2000/01/01"))
            {
                chkdisplay.Checked = true; ;
            }
            listGroup.Enabled = false;

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            listGroup.Enabled = true;
            txtGroupTitle2.Text = string.Empty;
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(txtGroupTitle2.Text.Trim()))
            {
                string oldTitle = listGroup.Text;
                string newTitle = txtGroupTitle2.Text;
                BLL.Group bll = new BLL.Group();
                Model.Group model = bll.GetModel(oldTitle);
                model.Title = newTitle;
                model.Pin = newTitle.toQuanPin();
                model.AddTime = Convert.ToDateTime("1900/01/01");
                if (chkdisplay.Checked)
                {
                    model.AddTime = DateTime.Now;
                }
                
                bll.Update(model);                
            }
            txtGroupTitle2.Text = string.Empty;
            listGroup.Enabled = true;
            DataBindClass.GroupBind(listGroup);

        }
        /// <summary>
        /// 上下排序调整
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOrder_Click(object sender, EventArgs e)
        {
            if (listGroup.SelectedIndex > -1)
            {
                string title = listGroup.Text;
                BLL.Group bll = new BLL.Group();
                Model.Group model = bll.GetModel(title);
                Button btn = (Button)sender;
                if (btn.Text == "上")
                {
                    if (model.OrderID > 0)
                    {
                        model.OrderID = model.OrderID - 1;
                    }
                }
                else
                {
                    model.OrderID = model.OrderID + 1;   
                }
                bll.Update(model);
                DataBindClass.GroupBind(listGroup);
                listGroup.SelectedItem = title;
            }
            else
            {
                MessageBox.Show("选择一个分类项");
            }

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            string groupTitle = txtGroupTitle2.Text;
            if (listGroup.Text == groupTitle)
            {
                BLL.Group bll = new global::TuKu.BLL.Group();
                Model.Group model = bll.GetModel(groupTitle);
                if (model != null)
                {
                    if (new BLL.subGroup().GetList(" groupid = " + model.ID.ToString()).Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show("存在子类，无法删除");
                    }
                    else
                    {
                        bll.Delete(int.Parse(model.ID.ToString()));
                    }
                }
                txtGroupTitle2.Text = string.Empty;
                DataBindClass.GroupBind(listGroup);
            }
            else
            {
                MessageBox.Show("请先择分类");
            }

            
        }

        private void listGroup_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
